﻿using AutoMapper;
using BW.MesProject.Public.Application.Dto.Input;
using BW.MesProject.Public.Application.Dto.Output;
using BW.MesProject.Public.Domain.RBAC;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using MD5Hash;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.MesProject.Public.Application
{
    /// <summary>
    /// 用户服务实现
    /// </summary>
    public class UserServices : IUserServices
    {
        private readonly IBaseRepository<UserModel> _userRepository;
        private readonly IMapper _mapper;
        private readonly IJwtService _jwtService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="UserRepository"></param>
        /// <param name="mapper"></param>
        public UserServices(IBaseRepository<UserModel> UserRepository, IMapper mapper, IJwtService jwtService)
        {
            _userRepository = UserRepository;
            this._mapper = mapper;
            this._jwtService = jwtService;
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<UserLoginOutput> UserLogin(UserLoginInput input)
        {
            //判断用户名称和密码是否为空
            if (string.IsNullOrEmpty(input.UserName) || string.IsNullOrEmpty(input.Password))
            {
                return new ApiResult<UserLoginOutput>()
                {
                    Code = ApiEnums.Fail,
                    Message = "用户名称和密码不能为空"
                };
            }
            try
            {
                //密码加密Md5
                var pwd = input.Password.GetMD5();
                //根据用户名称和密码获取用户信息
                var userModel = _userRepository.GetAll().First(x => x.UserName == input.UserName && x.Password == pwd);
                //判断用户是否存在
                if (userModel == null)
                {
                    return new ApiResult<UserLoginOutput>
                    {
                        Code = ApiEnums.Fail,
                        Message = "用户不存在或密码错误",
                        Data = null
                    };
                }

                var data = _mapper.Map<UserLoginOutput>(userModel);

                // 生成JWT令牌
                string token = _jwtService.GenerateToken(
                    userModel.Id,
                    userModel.UserName,
                    userModel.RealName);

                // 将令牌设置到输出对象
                data.Token = token;

                return new ApiResult<UserLoginOutput>
                {
                    Code = ApiEnums.Success,
                    Message = "登录成功",
                    Data = data
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<UserLoginOutput>
                {
                    Code = ApiEnums.Error,
                    Message = $"登录失败: {ex.Message}",
                    Data = null
                };
            }
        }


    }
}
